home *** CD-ROM | disk | FTP | other *** search
-
- #include <Types.h>
- #include <Dialogs.h>
- #include <Errors.h>
- #include <Menus.h>
- #include <Memory.h>
- #include <Controls.h>
- #include <Quickdraw.h>
- #include <Resources.h>
- #include <ToolUtils.h>
- #include <LowMem.h>
- #include <QuickDrawText.h>
- #include <OSUtils.h>
- #include <Events.h>
- #include <Windows.h>
- #include <Fonts.h>
-
- #include "MercutioAPI.h"
- #include "PopupDialog.h"
-
- #define popupDLOGID 3001
- #define popup1Item 3
- #define popup2Item 7
- #define defaultItem 5
-
- #define enterKey 3
- #define helpKey 5
- #define deleteKey 8
- #define tabKey 9
- #define linefeedKey 10
- #define cr 13
-
- #define Off 0
- #define On 1
-
- #define periodKey 46
- #define slashKey 47
- #define questionmarkKey 63
-
- /* Define HiWrd and LoWrd macros for efficiency. */
- #define HiWrd(aLong) (((aLong) >> 16) & 0xFFFF)
- #define LoWrd(aLong) ((aLong) & 0xFFFF)
-
- /* Define TopLeft and BotRight macros for convenience. Notice the implicit
- dependency on the ordering of fields within a Rect */
- #define TopLeft(aRect) (* (Point *) &(aRect).top)
- #define BotRight(aRect) (* (Point *) &(aRect).bottom)
-
-
- enum {
- normalPopupID = 11,
- smallPopupID
- };
-
- typedef struct {
- MenuHandle hMenu;
- short selectedItem;
- Boolean smallFont;
- } PopupInfo, *PopupInfoPtr;
-
- typedef struct {
- PopupInfo normalPopupMenu;
- PopupInfo smallPopupMenu;
- } DialogGlobals, *DialogGlobalsPtr;
-
- long PopUpSelect (MenuHandle theMenu, Point popPt, short popupItem, Boolean useWFont);
-
- PopupInfoPtr ItemToMenuInfo (DialogPtr theDialog, short item);
- PopupInfoPtr ItemToMenuInfo (DialogPtr theDialog, short item) {
- DialogGlobalsPtr theGlobals;
-
- theGlobals = (DialogGlobalsPtr)GetWRefCon(theDialog);
- if (item == popup1Item) {
- return (&theGlobals->normalPopupMenu);
- } else {
- return (&theGlobals->smallPopupMenu);
- }
- }
-
-
- pascal void FakeClick (DialogPtr theDialog, short theButton);
- pascal void FakeClick (DialogPtr theDialog, short theButton) {
- GrafPtr port;
-
- short iType;
- Handle iHandle;
- Rect iRect;
- long finalTicks;
-
- GetPort(&port); /* save old port */
- SetPort(theDialog); /* make dialog port */
-
- GetDialogItem(theDialog, theButton, &iType, &iHandle, &iRect);
- HiliteControl((ControlHandle)iHandle, On);
- Delay(8, &finalTicks);
- HiliteControl((ControlHandle)iHandle, Off);
- SetPort(port); /* restore old port */
- }
-
-
- pascal void DrawDefaultButton (DialogPtr theDialog, short theButton);
- pascal void DrawDefaultButton (DialogPtr theDialog, short theButton) {
- GrafPtr port;
-
- short iType;
- Handle iHandle;
- Rect iRect;
- PenState ps;
-
- GetPort(&port); /* save old port */
- SetPort(theDialog); /* make dialog port */
- GetDialogItem(theDialog, theButton, &iType, &iHandle, &iRect);
- GetPenState(&ps);
-
- PenNormal();
- PenSize(3,3);
- InsetRect(&iRect, -4, -4);
- FrameRoundRect(&iRect, 16, 16);
- SetPenState(&ps);
-
- SetPort(port); /* restore old port */
- }
-
-
-
-
- pascal void UpdatePopUp (DialogPtr theDialog, short item);
- pascal void UpdatePopUp (DialogPtr theDialog, short item) {
- GrafPtr port;
- short iType;
- Handle iHandle;
- Rect iRect;
- PopupInfoPtr mInfo;
-
- GetPort(&port); /* save old port */
- SetPort(theDialog); /* make dialog port */
-
- mInfo = ItemToMenuInfo(theDialog, item);
- GetDialogItem(theDialog, item, &iType, &iHandle, &iRect);
-
- InsetRect(&iRect, -2, -2);
- EraseRect(&iRect);
- InsetRect(&iRect, 2, 2);
-
- if (mInfo->smallFont) {
- TextSize(9);
- TextFont(geneva);
- }
- MDEF_CalcItemSize(mInfo->hMenu, mInfo->selectedItem, &iRect);
- MoveTo(iRect.left + 2, iRect.bottom);
- LineTo(iRect.right, iRect.bottom);
- MoveTo(iRect.right, iRect.top + 2);
- LineTo(iRect.right, iRect.bottom);
-
- MoveTo(iRect.left + 6, iRect.bottom - 5); // move to text position
- FillRect(&iRect, &qd.white);
-
- MDEF_DrawItem(mInfo->hMenu, mInfo->selectedItem, iRect);
-
- FrameRect(&iRect);
- SetDialogItem(theDialog, item, iType, iHandle, &iRect);
-
- if (mInfo->smallFont) {
- TextSize(0);
- TextFont(0);
- }
- SetPort(port); /* restore old port */
- }
-
-
-
-
- void HandleMouseDown (DialogPtr theDialog, short itemHit);
- void HandleMouseDown (DialogPtr theDialog, short itemHit) {
- GrafPtr port;
- short iType;
- Handle iHandle;
- Rect iRect;
- PopupInfoPtr mInfo;
- Rect popupRect;
- long popupResult;
-
- GetPort(&port); /* save old port */
- SetPort(theDialog); /* make dialog port */
-
- if ((itemHit == popup1Item) | (itemHit == popup2Item)) {
- mInfo = ItemToMenuInfo(theDialog, itemHit);
- GetDialogItem(theDialog, itemHit, &iType, &iHandle, &iRect);
- SetRect(&popupRect, iRect.left, iRect.top, iRect.left + (**(mInfo->hMenu)).menuWidth + 4, iRect.bottom + 2);
- LocalToGlobal(&TopLeft(popupRect));
- LocalToGlobal(&BotRight(popupRect));
- InsetRect(&popupRect, 1, 1);
-
- if (mInfo->smallFont) {
- TextSize(9);
- TextFont(geneva);
- }
-
- popupResult = PopUpSelect(mInfo->hMenu, TopLeft(popupRect), mInfo->selectedItem, mInfo->smallFont);
-
- if (mInfo->smallFont) {
- TextSize(0);
- TextFont(0);
- }
-
- if ((popupResult > 0) && (LoWrd(popupResult) != mInfo->selectedItem)) {
- CheckItem(mInfo->hMenu, mInfo->selectedItem, FALSE);
- mInfo->selectedItem = LoWrd(popupResult);
- UpdatePopUp(theDialog, itemHit);
- }
- }
- SetPort(port); /* restore old port */
- }
-
-
-
- void HandleEvents (DialogPtr theDialog);
- void HandleEvents (DialogPtr theDialog) {
- DialogGlobalsPtr theGlobals;
- Boolean done;
- Boolean eventOccured;
- short itemHit;
- EventRecord theEvent;
- short theKey;
- WindowPtr whichWindow;
- short windowLoc;
- long matchedItem;
- short menuNum;
- short itemNum;
-
- theGlobals = (DialogGlobalsPtr)GetWRefCon(theDialog);
- done = FALSE;
-
- while (!done) {
- eventOccured = GetNextEvent(everyEvent, &theEvent);
- if (eventOccured) {
- if (IsDialogEvent(&theEvent)) {
- if (DialogSelect(&theEvent, &whichWindow, &itemHit)) {
- HandleMouseDown((DialogPtr)whichWindow, itemHit);
- }
- }
- switch (theEvent.what) {
- case keyDown:
- case autoKey: /* check for menukey equivalents */
- theKey = theEvent.message & charCodeMask;
- if (theKey == cr) {
- itemHit = ok;
- FakeClick(theDialog, itemHit);
- } else if ((theEvent.modifiers & cmdKey) && (theKey == periodKey)) { /* Command key down */
- itemHit = cancel;
- FakeClick(theDialog, itemHit);
- } else {
- matchedItem = MDEF_MenuKey(theEvent.message, theEvent.modifiers, theGlobals->normalPopupMenu.hMenu);
- if (matchedItem > 0) {
- menuNum = HiWord(matchedItem);
- itemNum = LoWord(matchedItem);
- if (menuNum == normalPopupID) {
- theGlobals->normalPopupMenu.selectedItem = itemNum;
- UpdatePopUp(theDialog, popup1Item);
- } else if (menuNum == smallPopupID) {
- theGlobals->smallPopupMenu.selectedItem = itemNum;
- UpdatePopUp(theDialog, popup2Item);
- }
- }
- }
- break;
-
- case mouseDown:
- windowLoc = FindWindow(theEvent.where, &whichWindow);
- switch ( windowLoc ) {
- case inMenuBar:
- SysBeep(1);
- break;
- case inSysWindow: /* let the system handle the mouseDown */
- SystemClick(&theEvent, whichWindow);
- break;
- case inContent:
- if ( whichWindow != FrontWindow() ) {
- SelectWindow(whichWindow);
- /*DoEvent(event);*/ /* use this line for "do first click" */
- }
- break;
- case inDrag: /* pass screenBits.bounds to get all gDevices */
- DragWindow(whichWindow, theEvent.where, &qd.screenBits.bounds);
- break;
- case inGoAway:
- if ( TrackGoAway(whichWindow, theEvent.where) )
- // DoCloseWindow(whichWindow); /* we donUt care if the user cancelled */
- break;
- case inGrow:
- // DoGrowWindow(whichWindow, theEvent);
- break;
- case inZoomIn:
- case inZoomOut:
- if ( TrackBox(whichWindow, theEvent.where, windowLoc) )
- // DoZoomWindow(whichWindow, windowLoc);
- break;
- }
- break;
- default:
- DialogSelect(&theEvent, &whichWindow, &itemHit);
- break;
- }
- }
- done = ((itemHit == ok) || (itemHit == cancel));
- }
- }
-
-
-
- void doPopupDialog () {
- DialogPtr theDialog;
- short iType;
- Handle iHandle;
- Rect iRect;
- DialogGlobals theGlobals;
-
- theDialog = GetNewDialog(popupDLOGID, NULL, (WindowPtr) -1L);
- if (theDialog == 0L) {
- SysBeep(1);
- } else {
- SetPort(theDialog);
- SetWRefCon(theDialog, (long)&theGlobals);
-
- theGlobals.normalPopupMenu.hMenu = GetMenu(normalPopupID);
- InsertMenu(theGlobals.normalPopupMenu.hMenu, -1L);
- CalcMenuSize(theGlobals.normalPopupMenu.hMenu);
- theGlobals.normalPopupMenu.selectedItem = 1;
- theGlobals.normalPopupMenu.smallFont = FALSE;
- GetDialogItem(theDialog, popup1Item, &iType, &iHandle, &iRect);
- SetDialogItem(theDialog, popup1Item, userItem, (Handle) NewUserItemProc(UpdatePopUp), &iRect);
-
- theGlobals.smallPopupMenu.hMenu = GetMenu(smallPopupID);
- InsertMenu(theGlobals.smallPopupMenu.hMenu, -1L);
- CalcMenuSize(theGlobals.smallPopupMenu.hMenu);
- theGlobals.smallPopupMenu.selectedItem = 1;
- theGlobals.smallPopupMenu.smallFont = TRUE;
- GetDialogItem(theDialog, popup2Item, &iType, &iHandle, &iRect);
- SetDialogItem(theDialog, popup2Item, userItem, (Handle) NewUserItemProc(UpdatePopUp), &iRect);
-
- GetDialogItem(theDialog, defaultItem, &iType, &iHandle, &iRect);
- SetDialogItem(theDialog, defaultItem, userItem, (Handle) NewUserItemProc(DrawDefaultButton), &iRect);
-
- //SetDItem(dlg, dDefault, kind, (Handle) ZoneListDraw, &r);
-
- //GetDItem(CPDialog, editItem, &itemType, &itemHandle, &itemRect);
- ShowWindow(theDialog);
- HandleEvents(theDialog);
-
- DeleteMenu(normalPopupID);
- DeleteMenu(smallPopupID);
-
- DisposeDialog(theDialog);
- }
- }
-
-
- long PopUpSelect (MenuHandle theMenu, Point popPt, short popupItem, Boolean useWFont)
- {
- short item;
- short itemMark;
- short oldSysFont, oldWMgrFont, oldCWMgrFont;
- short oldSysSize, oldWMgrSize, oldCWMgrSize;
- GrafPtr curPort, wMgrPort;
- CGrafPtr wMgrCPort;
- SysEnvRec theWorld;
-
- oldSysFont = LMGetSysFontFam();
- oldSysSize = LMGetSysFontSize();
-
- /* check if we need to use window font */
-
- GetPort (&curPort);
- if (curPort->txFont == oldSysFont && curPort->txSize == oldSysSize)
- useWFont = false; /* window font _is_ system font! */
-
- if (useWFont) {
- /* hack to fix bugs caused by programs that mess up the WindowMgr port(s) */
- /* (e.g. MacWrite & Word) - thanks to Leonard Rosenthal for soln */
- GetWMgrPort (&wMgrPort);
- SetPort (wMgrPort);
- oldWMgrFont = wMgrPort->txFont;
- oldWMgrSize = wMgrPort->txSize;
- TextFont (systemFont);
- TextSize (0);
-
- if (SysEnvirons (1, &theWorld) == noErr && theWorld.hasColorQD) {
- GetCWMgrPort (&wMgrCPort);
- SetPort ((GrafPtr)wMgrCPort);
- oldCWMgrFont = wMgrCPort->txFont;
- oldCWMgrSize = wMgrCPort->txSize;
- TextFont (systemFont);
- TextSize (0);
- //TextFont (wMgrCPort->txFont);
- //TextSize (wMgrCPort->txSize);
- }
- else
- theWorld.hasColorQD = false;
-
-
- SetPort (curPort);
-
- LMSetSysFontFam(curPort->txFont);
- LMSetSysFontSize(curPort->txSize);
- LMSetLastSPExtra(-1L);
-
- }
-
- if (popupItem > 0) { /* save old mark and check item */
- GetItemMark (theMenu, popupItem, &itemMark);
- SetItemMark (theMenu, popupItem, useWFont? '*' : checkMark);
- }
-
- item = PopUpMenuSelect (theMenu, popPt.v, popPt.h, (popupItem>0) ? popupItem : 1);
-
- if (popupItem > 0) /* restore old item mark */
- SetItemMark (theMenu, popupItem, itemMark);
-
- if (useWFont) {
- SetPort (wMgrPort);
- TextFont (oldWMgrFont);
- TextSize (oldWMgrSize);
- if (theWorld.hasColorQD) {
- SetPort ((GrafPtr)wMgrCPort);
- TextFont (oldCWMgrFont);
- TextSize (oldCWMgrSize);
- }
- SetPort(curPort);
-
- LMSetSysFontSize(oldSysSize);
- LMSetSysFontFam(oldSysFont);
- LMSetLastSPExtra(-1L);
- }
-
- return item;
- }
-
-
- /*
- GetPort(&savePort);
- GetWMgrPort(&wmPort);
- GetCWMgrPort(&wmCPort);
-
- saveFont = wmPort->txFont;
- saveSize = wmPort->txSize;
- saveFace = wmPort->txFace;
-
- saveCFont = wmCPort->txFont;
- saveCSize = wmCPort->txSize;
- saveCFace = wmCPort->txFace;
-
- SetPort(wmPort);
- TextFont(applFont);
- TextSize(9);
- TextFace(0);
-
- SetPort((GrafPtr)wmCPort);
- TextFont(applFont);
- TextSize(9);
- TextFace(0);
- SetPort(savePort);
-
- sysFontSize = LMGetSysFontSize();
- LMSetSysFontSize(9);
-
- sysFontFamily = LMGetSysFontFam();
- LMSetSysFontFam(applFont);
-
- LMSetCurFMFam(-1);
-
- Then after popupmenuselect, I set everything back to how it was before:
- LMSetSysFontSize(sysFontSize);
- LMSetSysFontFam(sysFontFamily);
- LMSetCurFMFam(-1);
-
- SetPort(wmPort);
- TextFont(saveFont);
- TextSize(saveSize);
- TextFace(saveFace);
-
- SetPort((GrafPtr)wmCPort);
- TextFont(saveCFont);
- TextSize(saveCSize);
- TextFace(saveCFace);
-
- SetPort(savePort);
-
- */
-